<template>
	<view>
		<view class="top-bg"></view>
		<view class="content-wrap">
			<view class="form-wrap">
				<view class="form-item yf-flex-left">
					<view class="form-label">
						<text>*</text>开始时间：
					</view>
					<view class="uni-input" @click="show(1)">{{formData.startTime||"请选择"}}</view>
					<tui-icon name="arrowright" size="20"></tui-icon>
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						<text>*</text>结束时间：
					</view>
					<view class="uni-input" @click="show(2)">{{formData.endTime||"请选择"}}</view>
					<tui-icon name="arrowright" size="20"></tui-icon>
				</view>
				<view class="form-item yf-flex">
					<view class="form-label" style="padding-top: 20rpx;">
						<text>*</text>外出事由：
					</view>
					<view class="uni-input" style="padding: 20rpx 0;">
						<textarea name="" v-model="formData.desc" maxlength="100" placeholder="请输入外出事由"
							class="textarea"></textarea>
						<view class="desc-num">
							{{formData.desc.length}}/100
						</view>
					</view>
				</view>
				<view class="form-item yf-flex">
					<view class="form-label" style="padding-top: 20rpx;">
						请上传图片：
					</view>
					<!-- <input type="text" v-model="formData.name" placeholder="请输入部门" class="uni-input"> -->
					<view class="uni-input" style="padding: 20rpx 0;">
						<tui-upload width="120" height="120" :serverUrl="serverUrl" @complete="result" @remove="remove"
							:limit="1" :value="formData.img?[imagePath+formData.img]:''"></tui-upload>
					</view>
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						<text>*</text>部门:
					</view>
					<picker @change="bindPickerChange" range-key="name" :value="index" :range="deptList"
						class="uni-input">
						{{deptList[index].name||"请选择部门"}}
					</picker>
					<tui-icon name="arrowright" size="20"></tui-icon>
				</view>
				<view class="form-item">
					<view class="form-label" style="padding: 20rpx 0;">
						流程：
					</view>
					<view class="flow-wrap">
						<view class="flow-item yf-flex-left">
							<view class="flow-left yf-flex-vertical">
								<view class="flow-left-dot"></view>
								<view class="flow-left-line"></view>
							</view>
							<view class="flow-center">
								<view class="flow-center-title">审批人</view>
								<view class="flow-center-text">请选择审批人</view>
							</view>
							<view class="flow-right">
								<view class="user-info yf-flex-center" v-if="flowObj">
									<image
										:src="`${flowObj.avatar?imagePath+flowObj.avatar:'/static/common/head_img.png'}`"
										mode="aspectFill" class="user-img"></image>
									<view class="user-name">
										{{flowObj.userName}}
									</view>
									<tui-icon name="shut" size="12" color="#1684FC" @click="flowObj=''"></tui-icon>
								</view>
								<view class="yf-flex add-icon-wrap" v-else>
									<text style="color:#FF0000">*</text>
									<image src="/static/common/add_pic.png" mode="" class="add-icon"
										@click="jumpTo('addressList',{deptId:formData.deptId})"></image>
								</view>

							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="yf-flex-center submitBt" @click="formSubmit">提交</view>
		</view>
		<tui-datetime ref="dateTime" @confirm="change"></tui-datetime>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import indexConfig from "@/config/index.config.js"
	export default {
		data() {
			return {
				serverUrl: indexConfig.baseUrl + "api/punch/uploadPic",
				formData: {
					approverUserId: '', //审批人
					startTime: "", //开始时间
					endTime: "", //结束时间
					deptId: "", //部门Id
					desc: "", //外出事由
					img: "", //图片
					userId: "",
				},
				deptList: [], //部门列表
				index: "",
				dateType: 1, //1开始时间2结束时间
				flowObj: "", //审批人 
			};
		},
		computed: {
			...mapState(['imagePath', 'userInfo'])
		},
		onLoad() {
			//获取部门列表
			this.getDeptList();
			uni.$on('updateUser', (e) => {
				console.log(e, 22222222)
				this.flowObj = e;
			})

		},
		onUnload() {
			uni.$off('updateUser')
		},
		methods: {
			// 跳转
			jumpTo(url, param) {
				if (!param.deptId) {
					this.$api.msg("请先选择部门")
					return
				}
				this.$Router.navigateTo(url, param)
			},
			// 上传图片返回
			result(e) {
				if (e.imgArr.length > 0&&e.status==1) {
					this.formData.img = e.imgArr[0];
				}
			},
			// 删除图片回调
			remove(e) {
				this.formData.img = "";
			},
			// 部门列表
			getDeptList() {
				this.$apis.deptList().then((res) => {
					console.log(res, '@@@@@@@@')
					if (res.code == 0) {
						this.deptList = res.data
					}
				})
			},
			formSubmit: function() {
				let param = JSON.parse(JSON.stringify(this.formData))
				param.userId = this.userInfo.userId
				if (this.flowObj) {
					param.approverUserId = this.flowObj.procuratorId
				} else {
					param.approverUserId = ""
				}
				if (!param.startTime) {
					this.$api.msg("请选择开始时间")
					return
				}
				if (!param.endTime) {
					this.$api.msg("请选择结束时间")
					return
				}
				if (new Date(param.endTime).getTime() < new Date(param.startTime).getTime()) {
					this.$api.msg("结束时间不能小于开始时间")
					return
				}
				if (!param.desc) {
					this.$api.msg("请填写外出事由")
					return
				}
				if (!param.deptId) {
					this.$api.msg("请选择部门")
					return
				}
				if (!param.approverUserId) {
					this.$api.msg("请选择审批人")
					return
				}
				param.startTime = param.startTime + ":00"
				param.endTime = param.endTime + ":00" 
				//验证通过！
				uni.showModal({
					title: '提交',
					content: '是否确认提交？',
					confirmColor: "#3291F8",
					success: (res) => {
						if (res.confirm) {
							this.$api.showLoading()
							this.$apis.outward(param).then((res) => {
								this.$api.hideLoading()
								console.log(res, '@@@@@@@@')
								if (res.code == 0) {
									this.$api.msg("提交成功~")
									setTimeout(() => {
										this.$Router.navigateBack()
									}, 1500)

								} else {
									this.$api.msg(res.msg)
								}
							}).catch(() => {
								setTimeout(()=>{
									this.$api.hideLoading()
								},1500)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			// 打开时间选择器
			show(type) {
				this.dateType = type;
				this.$refs.dateTime.show();
			},
			// 选择时间
			change(e) {
				//选择的结果
				console.log(e);
				if (this.dateType == 1) {
					this.formData.startTime = e.result;
				} else {
					this.formData.endTime = e.result;
				}
			},
			// 部门选择
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
				this.formData.deptId = this.deptList[e.detail.value].id
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F4F8FB;
	}

	.top-bg {
		background: #3291F8;
		width: 100%;
		height: 200rpx;
	}

	.content-wrap {
		padding: 0 30rpx 30rpx;
		margin-top: -168rpx;
		width: 100%;

		.form-wrap {
			border-radius: 20rpx;
			background: #fff;
			padding: 22rpx 18rpx 22rpx;
			box-shadow: 0px 2rpx 6rpx 0px rgba(180, 180, 180, 40);

			.form-item {
				border-bottom: 1rpx solid #c9c9c9;
				min-height: 107rpx;
				padding: 0 24rpx;

				.form-label {
					font-size: 12+24rpx;
					width: 200rpx;

					text {
						color: #FF0000;
					}
				}

				.uni-input {
					flex: 1;
					font-size: 12+24rpx;

					.textarea {
						height: 160rpx;
						width: 100%;
						font-size: 12+24rpx
					}

					.desc-num {
						text-align: right;
						color: #7b7b7b;
						font-size: 12+24rpx;
					}
				}

				.flow-wrap {
					.flow-item {
						.flow-left {
							align-items: center;
							justify-content: center;
							margin-right: 15rpx;

							.flow-left-dot {
								width: 20rpx;
								height: 20rpx;
								border-radius: 50%;
								border: 4rpx solid #9B9B9B;
							}

							.flow-left-line {
								height: 60rpx;
								border: 1rpx solid #fff;
							}
						}

						.flow-center {
							flex: 1;
							padding-bottom: 20rpx;

							.flow-center-title {
								font-size: 12+24rpx;
								color: #000;
							}

							.flow-center-text {
								font-size: 12+20rpx;
								color: #7b7b7b;
								margin-top: 10rpx;
							}
						}

						.flow-right {
							.add-icon-wrap {
								.add-icon {
									width: 60rpx;
									height: 60rpx;
								}

							}

							.user-info {
								background: #F2F2F2;
								border-radius: 20rpx;
								padding: 8rpx 16rpx;

								.user-img {
									width: 50rpx;
									height: 50rpx;
									margin-right: 10rpx;
								}

								.user-name {
									font-size: 12+24rpx;
									margin-right: 10rpx;
								}
							}
						}
					}
				}
			}

			.form-item:last-child {
				border: none
			}

		}

		.submitBt {
			margin-top: 26rpx;
			width: 100%;
			height: 80rpx;
			border-radius: 50rpx;
			color: #fff;
			background: #3291F8;
			font-size: 12+24rpx;
		}
	}
</style>